import { createRouter, createWebHistory } from "vue-router";
import HomeView from "../views/HomeView.vue";
import { showToast } from "vant";

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: "/",
      name: "homeview",
      component: HomeView,
      redirect: "/home",
      children: [
        {
          // 首页
          path: "/home",
          name: "home",
          component: () => import("../views/tabbar/Home/index.vue"),
        },
        {
          // 分类页面
          path: "/cls",
          name: "cls",
          component: () => import("../views/tabbar/Cls/index.vue"),
        },
        {
          // 我的文章
          path: "/article",
          name: "article",
          component: () => import("../views/tabbar/Article/index.vue"),
        },
        {
          // 我的学习
          path: "/learn",
          name: "learn",
          component: () => import("../views/tabbar/Learn/index.vue"),
          meta: { requiresAuth: true }
        },
        {
          // 我的页面
          path: "/my",
          name: "my",
          component: () => import("../views/tabbar/My/index.vue"),
        },
      ],
    },
    {
      // 登录父级
      path: "/l",
      name: "l",
      component: () => import("../views/tabbar/Login/index.vue"),
      redirect: "/l/login",
      children: [
        {
          // 登录页
          path: "login",
          name: "login",
          component: () => import("../views/tabbar/Login/login/index.vue"),
        },
        {
          // 注册页
          path: "regis",
          name: "regis",
          component: () => import("../views/tabbar/Login/regis/index.vue"),
        },
        {
          // 手机号登录页
          path: "mobs",
          name: "mobs",
          component: () => import("../views/tabbar/Login/mobs/index.vue"),
        },
      ],
    },
    {
      //个人中心
      path: "/my/set",
      name: "set",
      component: () => import("../views/tabbar/My/views/SetView.vue"),
    },
    {
      // 个人中心信息
      path: "/my/set/setAccount",
      name: "setAccount",
      component: () => import("../views/tabbar/My/views/SetAccountView.vue"),
    },
    {
      // 拍照
      path: "/my/set/setAvatar",
      name: "setAvatar",
      component: () => import("../views/tabbar/My/components/MediaDevices.vue"),
    },
    {
      // 确定头像
      path: "/my/set/setAvatar/determineAvatar",
      name: "DetermineAvatar",
      component: () =>
        import("../views/tabbar/My/components/DetermineAvatar.vue"),
      props: (
        route: {
          query: { imgUrl: string };
        }) =>
      ({
        imgUrl: route.query.imgUrl
      })
    },
    { // 会话
      path: '/my/session',
      name: 'Session',
      component: () => import('../views/tabbar/My/views/SessionView.vue')
    },
    {
      path: '/my/session/:friendId',
      name: 'Chat',
      component: () => import('../views/tabbar/My/views/SessionItemView.vue'),
      props: (route:
        {
          params: { friendId: string };
        }) =>
      ({
        friendId: route.params.friendId
      })
    },
    { // 模拟登录
      path: '/my/login',
      name: 'DemoLogin',
      component: () => import('../views/tabbar/My/views/DemoLoginView.vue'),
      props: (route:
        {
          query: { imgUrl: string };
        }) =>
      ({
        imgUrl: route.query.imgUrl
      }),
    },

    {
      // 模拟登录
      path: "/my/login",
      name: "DemoLogin",
      component: () => import("../views/tabbar/My/views/DemoLoginView.vue"),
    },
    {
      // 搜索页面
      path: "/search",
      name: "search",
      component: () => import("../views/Search/index.vue"),
    },
    {
      // 文章详情页面
      path: "/details/:id",
      name: "details",
      component: () => import("../views/tabbar/Article/details/index.vue"),
    },

    {
      // 付费免费课程
      path: "/payfree",
      name: "payfree",
      component: () => import("../views/tabbar/Learn/payfree.vue"),
    },
    {
      // 缓存管理
      path: "/cache",
      name: "cache",
      component: () => import("../views/tabbar/Learn/cache.vue"),
    },
    {
      // 我的收藏
      path: "/coll",
      name: "coll",
      component: () => import("../views/tabbar/Learn/Collection.vue"),
    },
    {
      // 最近在学
      path: "/lastlearn",
      name: "lastlearn",
      component: () => import("../views/tabbar/Learn/Lastlearn.vue"),
    },
    {
      // 分类页
      path: "/plane",
      name: "plane",
      component: () => import("../views/tabbar/Cls/plane.vue"),
    },
    {
      // 详情页
      path: "/xiang",
      name: "xiang",
      component: () => import("../views/tabbar/Cls/xiang.vue"),
    },
    {
      //分类内容封装
      path: '/content',
      name: 'content',
      component: () => import('../views/tabbar/Cls/content.vue'),
    },
    {
      //视频播放页
      path: '/video',
      name: 'video',
      component: () => import('../views/tabbar/Cls/video.vue'),
    },
    {
      // 订单页
      path: "/orderform",
      name: "orderform",
      component: () => import("../views/tabbar/Cls/orderform.vue"),
      meta: { requiresAuth: true },
    },
    {
      // 课程列表详情页面
      path: "/courselist/:category",
      name: "courselist",
      component: () => import("../views/tabbar/Home/CourseList.vue"),
    },
    {
      path: "/404",
      component: () => import("../views/404/index.vue"),
      name: "404",
    },
    {
      path: "/:pathMatch(.*)",
      redirect: "/404",
      name: "any",
    },
    {
      // 课程播放
      path: "/vc",
      name: "vc",
      component: () => import("../views/tabbar/Learn/videocourse.vue"),
    },
    {
      path: '/demo',
      component: () => import('../views/tabbar/My/views/DemoView.vue')
    }
  ],
});

router.beforeEach((to, from, next) => {
  const tokenStr = localStorage.getItem("access_token");
  const requiresAuth = to.matched.some((record) => record.meta.requiresAuth);
  if (requiresAuth && !tokenStr) {
    showToast("哇哦，好像还没登陆呢！");
    return next("/l/login");
  }
  if (tokenStr && (to.path.startsWith("/l/login") || to.path.startsWith("/l/regis") || to.path.startsWith("/l/mobs"))) {
    showToast("您已登录，无需重复登录！");
    return next('/');
    // next()
  }
  // 其他情况放行
  next();
});

export default router;
